//
//  MakeAppointmentController.m
//  FinancialProduct
//  预约理财师
//  Created by Stella on 2017/8/25.
//  Copyright © 2017年 maq. All rights reserved.
//

#import "CKPuslichTool.h"
#import "CategoryCell.h"
#import "HandleNetProduct.h"
#import "UserModel.h"
#import "MakeAppointmentController.h"

@interface MakeAppointmentController ()<UITextViewDelegate>
@property (weak, nonatomic) IBOutlet UITextField *choosePeople;
@property (weak, nonatomic) IBOutlet UITextField *userCity;
@property (weak, nonatomic) IBOutlet UITextField *peopleHome;
@property (weak, nonatomic) IBOutlet UITextField *phoneName;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *peopleViewHeight;
@property (weak, nonatomic) IBOutlet UITextView *useLike;
@property (weak, nonatomic) IBOutlet UIButton *submitBt;
@property (weak, nonatomic) IBOutlet UILabel *reminder0;
@property (weak, nonatomic) IBOutlet UILabel *reminder1;
@property (weak, nonatomic) IBOutlet UILabel *reminder2;
@property (weak, nonatomic) IBOutlet UILabel *reminder3;
@property (weak, nonatomic) IBOutlet UILabel *reminder4;
@property (nonatomic, strong)NSArray *typeArray;
@property (weak, nonatomic) IBOutlet UILabel *userName;
@property (weak, nonatomic) IBOutlet UITableView *mTableView;
@property (weak, nonatomic) IBOutlet UIView *OKView;
@property (weak, nonatomic) IBOutlet UIButton *okBt;
@property (weak, nonatomic) IBOutlet UILabel *reminder;

@end

@implementation MakeAppointmentController
- (IBAction)backHome:(id)sender {
    
//    self.userName.text=@"";
//    NSDictionary *dic=@{@"index":@"0"};
//    [[NSNotificationCenter defaultCenter]postNotificationName:@"goToOtherView" object:nil userInfo:dic];
//    [self.navigationController dismissViewControllerAnimated:YES completion:nil];
    [self.navigationController popViewControllerAnimated:YES];
}

- (IBAction)choose:(id)sender {
    self.mTableView.hidden=!self.mTableView.hidden;
}

-(void)checkMyStatus{
    NSString *userId=[UserModel getUser].CustomerID;
    int mid=[userId intValue];
    NSDictionary *param=@{@"AAAMethodName":@"MakeFA",@"CustomerID":@(mid),@"MakeFAType":@"Select"};
    //NSLog(@"param==%@",param);
    [HandleNetProduct MakeFAWithParams:param WithAnimated:NO Success:^(NSDictionary *data) {
        //提交成功后显示OKview
        if ([data[@"msg"] isEqualToString:@"无预约"]) {
            self.OKView.hidden=YES;
            [self initMyView];
        }else{
            self.OKView.hidden=NO;
        }
        
    } Failure:^(NSString *errorInfo) {
        
    }];

}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.reminder0.text=@"";
    self.reminder1.text=@"";
    self.reminder2.text=@"";
    self.reminder3.text=@"";
    self.reminder4.text=@"";
    self.reminder.text=@"";
    
    
    
     [self checkMyStatus];
    
//    self.peopleHome.text=@"我也不知道啊";
//    self.phoneName.text=@"某某某";
//    self.useLike.text=@"高收益，低风险";
//    self.userCity.text=@"上海";
    
}
-(void)initMyView{
    self.userName.text=[UserModel getUser].CustomerName;
    _typeArray=@[@"指定理财师",@"就近分配"];
    self.choosePeople.text=@"请选择分配方式";
    [self setTextfile:self.choosePeople img:[UIImage imageNamed:@"Admin-User"]];
    [self setTextfile:self.peopleHome img:[UIImage imageNamed:@"jigou"]];
    [self setTextfile:self.phoneName img:[UIImage imageNamed:@"licai_make"]];
    [self setTextfile:self.userCity img:[UIImage imageNamed:@"chengshi"]];
    [self.peopleHome       addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
    [self.phoneName       addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
    [self.userCity       addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
    
    
    
    self.peopleViewHeight.constant=0;
    UIViewSetRadius(self.useLike, 5, 0, nil);
    UIViewSetRadius(self.submitBt, 5, 0, nil);
    UIViewSetRadius(self.okBt, 5, 0, nil);
    [self.mTableView reloadData];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (IBAction)submit:(id)sender {
    self.reminder1.text=@"";
    self.reminder2.text=@"";
    self.reminder3.text=@"";
    self.reminder4.text=@"";
    self.reminder.text=@"";
    self.reminder0.text=@"";
    BOOL ischoose=NO;
    if ([self.choosePeople.text isEqualToString:@"请选择分配方式"]) {
        self.reminder0.text=@"*请选择分配方式";
        return;
    }
    
    
    if ([self.choosePeople.text isEqualToString:@"指定理财师"]) {
        ischoose=YES;
        if (_peopleHome.text.length==0) {
            self.reminder1.text=@"*请输入理财师所在机构";
            return;
        }
      
        
        if (_phoneName.text.length==0) {
            self.reminder2.text=@"*请输入理财师姓名";
            return;
        }
       
    }
    
    
    if (_userCity.text.length==0) {
        self.reminder3.text=@"*请输入您所在的城市";
        return;
    }

    if (_useLike.text.length==0) {
        self.reminder4.text=@"*请输入您的投资偏好";
        return;
    }
    /*
     * SelectFAType 选择理财师（就近分配， 指定理财师）
     * AccountCity 用户所在城市
     * Preference 投资偏好
     
     ***********************8
     FAOrganization 理财师机构。当选择理财师是制定理财师时必填
     FAName   理财师名字。当选择理财师是制定理财师时必填
     
     
     */
    NSString *userId=[UserModel getUser].CustomerID;
    int mid=[userId intValue];
    NSDictionary *param;
    if (ischoose) {
        
        param=@{@"AAAMethodName":@"MakeFA",@"CustomerID":@(mid),
                @"SelectFAType":@"指定理财师",
                @"FAOrganization":_peopleHome.text,
                @"FAName":_phoneName.text,
                @"AccountCity":_userCity.text,@"Preference":_useLike.text,
                @"MakeFAType":@"Update"};
    }else{
        param=@{@"AAAMethodName":@"MakeFA",@"CustomerID":@(mid),
                @"SelectFAType":@"就近分配",
                @"AccountCity":_userCity.text,@"Preference":_useLike.text,
                @"MakeFAType":@"Update"};
    }
    [HandleNetProduct MakeFAWithParams:param WithAnimated:YES Success:^(NSDictionary *data) {
        //提交成功后显示OKview
        if ([data[@"msg"] isEqualToString:@"成功"]) {
            self.OKView.hidden=NO;
        }else{
            self.reminder.text=[NSString stringWithFormat:@"*%@",data[@"msg"]];
        }
        
    } Failure:^(NSString *errorInfo) {
        self.reminder.text=[NSString stringWithFormat:@"*%@",errorInfo];
    }];
    
    
}
- (void)textViewDidChange:(UITextView *)textView{
    if (textView.text.length>300) {
        textView.text = [textView.text substringToIndex:300];
    }
}
-(void)textFieldDidChange:(UITextField *)textField{
    if (textField==_userCity) {
        if (textField.text.length>50) {
            textField.text = [textField.text substringToIndex:50];
        }
    }else{
        if (textField.text.length>20) {
            textField.text = [textField.text substringToIndex:20];
        }
    }
    
    
}
- (IBAction)ok:(id)sender {

    [self.navigationController popViewControllerAnimated:YES];
//    NSDictionary *dic=@{@"index":@"0"};
//    [[NSNotificationCenter defaultCenter]postNotificationName:@"goToOtherView" object:nil userInfo:dic];
//    [self.navigationController dismissViewControllerAnimated:YES completion:nil];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:NO];
   
}
-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:NO];
}
#pragma mark - Table view data source
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 40;
    
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    
    return 1;
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    
    return _typeArray.count;
    
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    CategoryCell *cell=[tableView dequeueReusableCellWithIdentifier:@"CategoryCell" forIndexPath:indexPath];
    cell.selectionStyle=UITableViewCellSelectionStyleNone;
    cell.accessoryType=UITableViewCellAccessoryNone;
    cell.NameLabel.text=_typeArray[indexPath.row];
    return cell;
    
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 0.01f;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.01f;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    self.mTableView.hidden=YES;
    self.choosePeople.text=self.typeArray[indexPath.row];
    if ([self.choosePeople.text isEqualToString:@"指定理财师"]) {
        self.peopleViewHeight.constant=130;
    }else{
        self.peopleViewHeight.constant=0;
    }
}
-(void)setTextfile:(UITextField *)textfile img:(UIImage *)img{
    UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 50, 27)];
    imageView.image =img;
    imageView.contentMode = UIViewContentModeCenter;
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(imageView.frame.size.width-5, 6, 1, textfile.frame.size.height-6*2)];
    view.backgroundColor = [UIColor groupTableViewBackgroundColor];
    [textfile addSubview:view];
    
    textfile.leftView = imageView;
    textfile.leftViewMode = UITextFieldViewModeAlways;
    textfile.layer.cornerRadius = 5;
}

@end
